1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package cn.flightfeather.thirdapp.module.inspection
 
import android.app.Dialog
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import android.os.Environment
import android.support.design.widget.FloatingActionButton
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import android.widget.*
import cn.flightfeather.thirdapp.R
import cn.flightfeather.thirdapp.activity.GitDetailActivity
import cn.flightfeather.thirdapp.adapter.GitListAdapter
import cn.flightfeather.thirdapp.adapter.GitTypeListAdapter
import cn.flightfeather.thirdapp.adapter.RecyclerItemClickListener
import cn.flightfeather.thirdapp.bean.*
import cn.flightfeather.thirdapp.common.database.DbFactory
import cn.flightfeather.thirdapp.common.net.RetrofitFactory
import cn.flightfeather.thirdapp.httpservice.InspectionService
import cn.flightfeather.thirdapp.module.base.BaseTakePicActivity
import cn.flightfeather.thirdapp.util.DateFormatter
import cn.flightfeather.thirdapp.util.ScreenUtils
import cn.flightfeather.thirdapp.util.UUIDGenerator
import cn.flightfeather.thirdapp.util.file.FileUtil
import com.ping.greendao.gen.GittypeDao
import kotlinx.android.synthetic.main.dialog_camera.*
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.File
import java.io.IOException
import java.util.*
import kotlin.collections.ArrayList
 
class MenuNewGitActivity : BaseTakePicActivity() {
 
    override val picDeletable: Boolean = true
 
    override fun getImageViews(): MutableList<ImageView> = mutableListOf()
 
    override fun getLayoutId(): Int = R.layout.dialog_camera
 
    lateinit var viewModel: MenuNewGitViewModel
 
    var subTask: Subtask? = null
    var inspection: Inspection? = null
    var scene: Scense? = null
    var lat = 0.0
    var lng = 0.0
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(MenuNewGitViewModel::class.java)
 
        subTask = intent.getSerializableExtra("subTask") as Subtask?
        inspection = intent.getSerializableExtra("inspection") as Inspection?
        scene = intent.getSerializableExtra("scene") as Scense?
        lat = intent.getDoubleExtra("lat", 0.0)
        lng = intent.getDoubleExtra("lng", 0.0)
 
        initUI()
 
        viewModel.gitList.observe(this, Observer {
            it?.let {
                rv_photo_list.adapter?.notifyDataSetChanged()
 
                val t = "共${it.size}条技防措施"
                tv_count.text = t
            }
        })
 
        inspection?.guid?.let { viewModel.getGitList(it) }
    }
 
    private fun initUI() {
        tv_title.text = "技防措施"
 
        rv_photo_list.run {
            layoutManager = LinearLayoutManager(this@MenuNewGitActivity)
            adapter = GitListAdapter(this@MenuNewGitActivity, viewModel.gitList.value)
            addOnItemTouchListener(RecyclerItemClickListener(this@MenuNewGitActivity, this, object : RecyclerItemClickListener.OnItemClickListener {
                override fun onItemClick(view: View?, position: Int) {
                    val intent = Intent(this@MenuNewGitActivity, GitDetailActivity::class.java)
                    intent.putExtra("gitlistVo", viewModel.gitList.value?.get(position))
                    intent.putExtra("subTask", subTask)
                    intent.putExtra("inspectionGuid", inspection?.guid)
                    intent.putExtra("scenseAddress", "${scene?.cityname ?: ""} + ${scene?.districtname?:""} + ${scene?.townname?:""} + ${scene?.location?:""}")
                    startActivity(intent)
                }
 
                override fun onItemLongClick(view: View?, position: Int) {
                }
 
            }))
        }
 
        iv_new_photo.setOnClickListener{
            showNewGitDialog()
        }
 
        fab_problem_list_close.setOnClickListener {
            finish()
        }
    }
 
    //新增一条技防措施的dialog
    private fun showNewGitDialog() {
        val dialog = Dialog(this)
        dialog.setContentView(R.layout.dialog_take_evidence)
        //设置dialog宽度
        val dialogWindow = dialog.window
        dialogWindow!!.setBackgroundDrawableResource(android.R.color.transparent)
        val p = dialogWindow.attributes
        p.width = ScreenUtils.getScreenWidth(this) * 1
 
        dialog.setCancelable(false)
        //初始化控件
        val tv_title = dialog.findViewById(R.id.tv_dialog_take_evidence_title) as TextView
        val tv_type = dialog.findViewById(R.id.tv_type) as TextView
        val tv_chose = dialog.findViewById(R.id.tv_chose) as TextView
        val tv_location = dialog.findViewById(R.id.tv_location) as TextView
        val fab_ok = dialog.findViewById(R.id.fab_take_evidence_ok) as FloatingActionButton
        val fab_Close = dialog.findViewById(R.id.fab_take_evidence_close) as FloatingActionButton
        val iv_gitPhoto1 = dialog.findViewById(R.id.iv_take_evidence_add_photo1) as ImageView
        val iv_gitPhoto2 = dialog.findViewById(R.id.iv_take_evidence_add_photo2) as ImageView
        val iv_gitPhoto3 = dialog.findViewById(R.id.iv_take_evidence_add_photo3) as ImageView
        val sp_gitType = dialog.findViewById(R.id.sp_take_evidence_select_problem_type) as Spinner
        val sp_git = dialog.findViewById(R.id.sp_take_evidence_select_problem) as Spinner
        val sp_location = dialog.findViewById(R.id.sp_take_evidence_select_location) as Spinner
        val et_locationRemark = dialog.findViewById(R.id.et_take_evidence_location) as EditText
        val et_problemDes = dialog.findViewById(R.id.et_take_evidence_problem_des) as EditText
        val ll_change_suggestion = dialog.findViewById<LinearLayout>(R.id.ll_change_suggestion)
 
        imageViewList.apply {
            pathTempList.clear()
            clear()
            add(iv_gitPhoto1)
            add(iv_gitPhoto2)
            add(iv_gitPhoto3)
            refreshImageView()
        }
 
        //设置界面
        tv_title.text = "新增技防措施"
        tv_type.text = "措施类型"
        tv_chose.text = "选择一个技防措施"
        tv_location.visibility = View.GONE
        sp_location.visibility = View.GONE
        et_locationRemark.visibility = View.GONE
        et_problemDes.visibility = View.GONE
        ll_change_suggestion.visibility = View.GONE
        //加载预置数据
        val gittypeList = DbFactory.getInstance().gittypeDao.queryBuilder()
                .where(
                        GittypeDao.Properties.Tasktype.eq(subTask?.type ?: ""),
                        GittypeDao.Properties.Scensetype.eq(scene?.type ?: ""),
                        GittypeDao.Properties.Districtname.eq(subTask?.districtname ?: "")
                ).orderAsc(GittypeDao.Properties.Typeid).list()
        if (gittypeList.size == 0) {
            val gittype1 = Gittype()
            gittype1.guid = "00"
            gittype1.name = "无"
            gittype1.type = "无"
            gittype1.desc = "无"
            gittypeList.add(gittype1)
        }
 
        val gitTypeStringList = ArrayList<String>()
        val gitTypeMap = HashMap<String, ArrayList<Gittype>>()
        for (gittype in gittypeList) {
            if (gitTypeMap.containsKey(gittype.getType())) {
                gitTypeMap[gittype.type]!!.add(gittype)
            } else {
                gitTypeStringList.add(gittype.type)
                val gittypeList1 = ArrayList<Gittype>()
                gittypeList1.add(gittype)
                gitTypeMap[gittype.type] = gittypeList1
            }
        }
        val gittypeAdapter = ArrayAdapter(this, R.layout.item_spinner_drop_down, gitTypeStringList)
        sp_gitType.adapter = gittypeAdapter
        val gittypeList1 = ArrayList<Gittype>()
        val gitTypeListAdapter = GitTypeListAdapter(gittypeList1, this)
        sp_git.adapter = gitTypeListAdapter
        sp_gitType.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {
                //当选中一个分类时,从map加载问题列表,填充数据
                gittypeList1.removeAll(gittypeList1)
                gittypeList1.addAll(gitTypeMap[gitTypeStringList[i]]!!)
                gitTypeListAdapter.notifyDataSetChanged()
            }
 
            override fun onNothingSelected(adapterView: AdapterView<*>) {
 
            }
        }
 
 
        fab_ok.setOnClickListener {
            if (pathTempList.size > 0) {
                //准备gitlist
                val gitlist = Gitlist()
                gitlist.guid = UUIDGenerator.generate16ShortUUID()
                gitlist.iguid = inspection?.guid
                gitlist.stguid = subTask?.stguid
                gitlist.sguid = scene?.guid
                gitlist.scensename = scene?.name
                gitlist.scenseaddress = "${scene?.cityname ?: ""} + ${scene?.districtname?:""} + ${scene?.townname?:""} + ${scene?.location?:""}"
                val gitSelected = sp_git.selectedItem as Gittype
                gitlist.gtguid = gitSelected.guid
                gitlist.name = gitSelected.name
                gitlist.type = gitSelected.type
                gitlist.desc = gitSelected.desc
                gitlist.createdate = Date()
                gitlist.updatedate = Date()
                //准备mediaFIle
                val calendar = java.util.Calendar.getInstance()
                calendar.time = subTask?.executionstarttime
                val path = "FlightFeather/Photo/" + scene?.districtname + "/" + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月/" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日/" + scene?.name + "/技防措施/"
 
                val putGit = RetrofitFactory.instance.retrofit.create(InspectionService::class.java).putGitList(gitlist)
                putGit.enqueue(object : Callback<ResponseBody> {
                    override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                        if (response.body() != null) {
                            Toast.makeText(application, "提交成功", Toast.LENGTH_SHORT).show()
                            for (oldfile in pathTempList) {
                                val fileName = gitSelected.name + " " + UUIDGenerator.generateUUID(4) + ".jpg"
                                val mediafile = Mediafile()
                                mediafile.guid = UUIDGenerator.generate16ShortUUID()
                                mediafile.businessguid = gitlist.guid
                                mediafile.iguid = inspection?.guid
                                mediafile.longitude = lng
                                mediafile.latitude = lat
                                mediafile.address = "${scene?.cityname ?: ""} + ${scene?.districtname?:""} + ${scene?.townname?:""} + ${scene?.location?:""}"
                                mediafile.filetype = 1
                                mediafile.businesstype = "2"
                                mediafile.businesstype = "技防措施"
                                mediafile.path = path
                                mediafile.description = fileName
                                mediafile.savetime = Date()
                                val exetension1 = scene?.getCitycode() + "/" + scene?.getDistrictcode() + "/" + DateFormatter.dateFormat2.format(calendar.time) + "/" + scene?.getGuid() + "/"
                                mediafile.extension1 = exetension1
                                mediafile.remark = "未上传"
                                try {
                                    val newfile = File(Environment.getExternalStorageDirectory(), path + fileName)
                                    newfile.parentFile.mkdirs()
                                    FileUtil.copyFile(oldfile, newfile)
 
                                    viewModel.putMediaFile(mediafile)
                                } catch (e: IOException) {
                                    e.printStackTrace()
                                }
 
                            }
                            dialog.dismiss()
                            inspection?.guid?.let { viewModel.getGitList(it) }
                        } else if (response.errorBody() != null) {
                            Log.e("putGit:", response.errorBody()!!.toString())
                            Toast.makeText(application, "提交失败", Toast.LENGTH_SHORT).show()
                        }
                    }
 
                    override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                        Log.e("putGit:", t.toString())
                        Toast.makeText(application, "联网失败", Toast.LENGTH_SHORT).show()
                    }
                })
 
            } else {
                Toast.makeText(application, "至少拍一张照片", Toast.LENGTH_SHORT).show()
            }
        }
        fab_Close.setOnClickListener { dialog.dismiss() }
        dialog.show()
    }
}